<?php
function func_response($str = '')
{
	echo $str;exit;
}
function func_get($str){
	return isset($_GET[$str]) ? $_GET[$str] : '';
}
function func_request($str){
	return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
function func_post($str){
	return isset($_POST[$str]) ? $_POST[$str] : '';
}
require('./db.php');
session_start();
$u_id = $_SESSION['u_id'];
?>
<meta charset="utf-8" >
<meta http-equiv="content-type" content="text/html" >
<META HTTP-EQUIV="Pragma"   CONTENT="no-cache">
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width=device-width"   />
<meta name="format-detection" content="telephone=no" />
<meta name="app-mobile-web-app-capable"  content="yes" />
<meta name="app-mobile-web-app-status-bar-style" content="black-translucent" />
<script>
    function postInfos(URL,postStr,functions,s){
        var xmlHttp=xmlInit();
        xmlHttp.open("POST", URL, true);
        xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
        xmlHttp.setRequestHeader("Content-TYPE","application/x-www-form-urlencoded");
        xmlHttp.send(postStr);
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                var pReturn="";
                pReturn=xmlHttp.responseText;
                functions(pReturn,s);
            }
        }

    }
    function xmlInit(){
        var xmlHttp = false;
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp =  new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                xmlHttp =  false;
            }
        }
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp =  new XMLHttpRequest();
        }
        return xmlHttp;
    }
</script>
<?php
$sql = "select id,is_admin from lyl_users where id=" . $u_id;
$user = Database::query($sql);
if ($user) {
	$is_admin = 0;
	if($user[0]['is_admin']) {
		$is_admin = 1;
	}
} else {
	$_SESSION['u_id'] = null;
	header("Location: /");
	exit;
}

?>
<div>
    <style>
        .box{
            display: inline-block;
            border: 1px solid;
            padding: 10px;
            border-radius: 5px;
            vertical-align: top;
        }
        .edit_box{
            position: fixed;
            width: 100%;
            height: 100%;
            background: #cccccc80;
            top: 0;
            display: none;
        }
        .edit_box>div{
            margin: auto;
            text-align: center;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            top: 50%;
            background: #fff;
        }
    </style>
    <?php if($is_admin) {?>
    <div class="box">
        <input id="t"  type="hidden" name="t" value='add_sing' />
        <p>名称: <input id='name' type="text" name="name" /></p>
        <p>编号: <input id='code' type="text" name="code" /></p>
        <p>类型: <input id='type' type="text" name="type" /></p>
        <p>总数: <input id='num' type="text" name="num" /></p>
        <p>单价: <input id='price' type="text" name="price" /></p>
        <input type="button" value="添加" onclick="submit()"/>
    </div>
    <script>
        function submit(){
            var t = document.getElementById("t");
            var name = document.getElementById("name");
            var code = document.getElementById("code");
            var type = document.getElementById("type");
            var num = document.getElementById("num");
            var price = document.getElementById("price");
            if(name.value === '') {
                alert('名称不能为空');
                return false;
            }
            if(code.value === '') {
                alert('编号不能为空');
                return false;
            }
            if(type.value === '') {
                alert('类型不能为空');
                return false;
            }
            if(num.value === '') {
                alert('总数不能为空');
                return false;
            }
            if(price.value === '') {
                alert('单价不能为空');
                return false;
            }
            var url = "api.php";
            var params= "t=" + t.value + "&name=" + name.value;
            params += "&code=" + code.value;
            params += "&type=" + type.value;
            params += "&num=" + num.value;
            params += "&price=" + price.value;
            postInfos(url,params,function (res) {
                if(res === '添加成功') {
                    alert(res);
                    window.location.reload();
                    return false;
                }
                alert(res);
            })
            return false;
        }
        function e_close() {
            document.getElementById('edit_box').style.display = 'none';
        }
        function e_show() {
            document.getElementById('edit_box').style.display = 'block';
        }
    </script>
    <div class="edit_box" id="edit_box">
        <div class="box" >
            <form>
                <input id="e_t"  type="hidden" name="t" value='2' />
                <p>ID: <input id='e_id' type="text" name="id" disabled /></p>
                <p>名称: <input id='e_name' type="text" name="name" /></p>
                <p>编号: <input id='e_code' type="text" name="code" /></p>
                <p>类型: <input id='e_type' type="text" name="type" /></p>
                <p>总数: <input id='e_num' type="text" name="num" /></p>
                <p>单价: <input id='e_price' type="text" name="price" /></p>
                <input type="button" value="确认编辑" onclick="e_submit()"/>
                <input type="button" value="关闭" onclick="e_close()"/>
            </form>
        </div>
    </div>
    <script>
        function e_submit(){
            var t = document.getElementById("e_t");
            var id = document.getElementById("e_id");
            var name = document.getElementById("e_name");
            var code = document.getElementById("e_code");
            var type = document.getElementById("e_type");
            var num = document.getElementById("e_num");
            var price = document.getElementById("e_price");
            if(name.value === '') {
                alert('名称不能为空');
                return false;
            }
            if(code.value === '') {
                alert('编号不能为空');
                return false;
            }
            if(type.value === '') {
                alert('类型不能为空');
                return false;
            }
            if(num.value === '') {
                alert('总数不能为空');
                return false;
            }
            if(price.value === '') {
                alert('单价不能为空');
                return false;
            }
            var url = "api.php";
            var params= "t=" + t.value + "&name=" + name.value;
            params += "&code=" + code.value;
            params += "&type=" + type.value;
            params += "&num=" + num.value;
            params += "&price=" + price.value;
            params += "&id=" + id.value;
            postInfos(url,params,function (res) {
                if(res === '编辑成功') {
                    alert(res);
                    window.location.reload();
                    return false;
                }
                alert(res);
            })
            return false;
        }
        function edit(id,name,code,type,num,price) {
            document.getElementById("e_id").value = id;
            document.getElementById("e_name").value = name;
            document.getElementById("e_code").value = code;
            document.getElementById("e_type").value = type;
            document.getElementById("e_num").value = num;
            document.getElementById("e_price").value = price;
            e_show();
        }
    </script>
    <?php } ?>
</div>
<?php


$p = func_request("p");
$size = 20;
if(!$p) {
	$p = 1;
}
if ($is_admin) {
	$sql = "select count(id) c from lyl_singleton";
} else {
	$sql = "select count(id) c from lyl_singleton where `use`=1";
}
$count_rs = Database::query($sql);
if (!$count_rs) {
	$c = 0;
} else {
	$c = $count_rs[0]['c'];
}
require('./page.php');
$page = new Page(10,$c,3);
if ($is_admin) {
    $sql = "select * from lyl_singleton limit " .$page->offestPage.','.$page->colNum;
} else {
    $sql = "select * from lyl_singleton where `use`=1 limit " .$page->offestPage.','.$page->colNum;
}
$list = Database::query($sql);
if (!$list) {
	$list = [];
}
?>
<script>
    function out() {
        var url = "api.php";
        var params= "t=out";
        postInfos(url,params,function (res) {
            alert(res);
            window.location.reload();
        })
    }
</script>
<?php if($is_admin) { ?>
    <a href="/">信息列表</a>
    <a href="/src/user.php">人员列表</a>
    <a href="/src/sing.php">信息列表</a>
<?php } ?>
<a href="javascript:;" onclick="out()">退出</a>

<style>
    #hor-zebra
    {
        font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
        font-size: 12px;
        width: 100%;
        text-align: left;
        border-collapse: collapse;
    }
    #hor-zebra th
    {
        font-size: 14px;
        font-weight: normal;
        padding: 10px 8px;
        color: #039;
    }
    #hor-zebra td
    {
        padding: 8px;
        color: #669;
    }
    #hor-zebra tr
    {
        border-bottom: 1px solid #e62f2f;
    }
    #hor-zebra .odd
    {
        background: #e8edff;
    }
</style>
<h2 style="text-align: center">信息列表</h2>
<script>
    function toAjax(url) {
        postInfos(url,'',function (res) {
            window.location.reload();
        });
    }
</script>
<div style="text-align: center;">
    <?=$page->show();?>
</div>
<table id="hor-zebra" summary="Employee Pay Sheet">
    <thead>
    <tr>
        <th scope="col">id</th>
        <th scope="col">名称</th>
        <th scope="col">编号</th>
        <th scope="col">类型</th>
        <th scope="col">总数</th>
        <th scope="col">已填</th>
        <th scope="col">单价</th>
        <th scope="col">操作</th>
    </tr>
    </thead>
    <tbody>
	<?php foreach ($list as $item) { ?>
        <tr class="odd">
            <td><?=$item["id"] ?> </td>
            <td><?=$item["name"] ?></td>
            <td><?=$item["code"] ?></td>
            <td><?=$item["type"] ?></td>
            <td><?=$item["num"] ?></td>
            <td><?=$item["n"]  ?></td>
            <td><?=$item["price"] ?></td>
            <td>
                <?php if($is_admin) { ?>
                    <?php if($item["use"] == 1) { ?>
                        <input type="button" value="隐藏" onclick="toAjax('./api.php?t=1&use=0&id=<?=$item["id"]?>')">
                    <?php } else { ?>
                        <input type="button" value="显示" onclick="toAjax('./api.php?t=1&use=1&id=<?=$item["id"]?>')">
                    <?php } ?>
                        <input type="button" value="编辑" onclick="edit('<?=$item["id"] ?>','<?=$item["name"] ?>','<?=$item["code"] ?>','<?=$item["type"] ?>','<?=$item["num"] ?>','<?=$item["price"] ?>')">
                <?php } else {?>
                    <input type="button" value="填写单子" onclick="add(<?=$item["id"] ?>)">
                    <?php
                    $sql = "select id,num from lyl_bill where s_id=" . $item["id"] . " and u_id=" . $u_id;
                    $rs = Database::query($sql);
                    if(empty($rs)) {
                        $n = 0;
                    } else {
                        $n = $rs[0]['num'];
                    }
                    ?>
                    此账户已填<?=$n ?>单
                <?php } ?>

            </td>
        </tr>
	<?php } ?>
    </tbody>
</table>
<script>
    function add(id) {
        var num = prompt("请输入，填入单子数量");
        if(num >= 0) {
            postInfos("api.php", "t=3&id=" + id + "&num="+num,function (res) {
                if(res === '成功') {
                    window.location.reload();
                } else {
                    alert(res)
                }
            })
        }
    }
</script>


